package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.vo.TechnologicalProcessVO;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.domain.ProjectInfo;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.TechnologicalProcess;
import com.ruoyi.base.service.ITechnologicalProcessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工艺流程Controller
 *
 * @author ruoyi
 * @date 2022-08-16
 */
@RestController
@Api(value = "工艺流程", tags = "工艺流程")
@RequestMapping("/base/process")
public class TechnologicalProcessController extends BaseController {
    @Autowired
    private ITechnologicalProcessService technologicalProcessService;

    /**
     * 查询工艺流程列表
     */
    @PreAuthorize("@ss.hasPermi('base:process:list')")
    @GetMapping("/list")
    public TableDataInfo list(TechnologicalProcess technologicalProcess) {
        startPage();
        List<TechnologicalProcess> list = technologicalProcessService.selectTechnologicalProcessList(technologicalProcess);
        return getDataTable(list);
    }

    /**
     * 导出工艺流程列表
     */
    @PreAuthorize("@ss.hasPermi('base:process:export')")
    @Log(title = "工艺流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TechnologicalProcess technologicalProcess) {
        List<TechnologicalProcess> list = technologicalProcessService.selectTechnologicalProcessList(technologicalProcess);
        ExcelUtil<TechnologicalProcess> util = new ExcelUtil<TechnologicalProcess>(TechnologicalProcess.class);
        util.exportExcel(response, list, "工艺流程数据");
    }

    /**
     * 获取工艺流程详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:process:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(technologicalProcessService.selectTechnologicalProcessById(id));
    }

    /**
     * 新增工艺流程
     */
    @PreAuthorize("@ss.hasPermi('base:process:add')")
    @Log(title = "工艺流程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TechnologicalProcessVO technologicalProcessVO) {
        checkRequiredFields(technologicalProcessVO);
        return toAjax(technologicalProcessService.insertTechnologicalProcess(technologicalProcessVO));
    }

    /**
     * 修改工艺流程
     */
    @PreAuthorize("@ss.hasPermi('base:process:edit')")
    @Log(title = "工艺流程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TechnologicalProcessVO technologicalProcessVO) {
        checkRequiredFields(technologicalProcessVO);
        return toAjax(technologicalProcessService.updateTechnologicalProcess(technologicalProcessVO));
    }
    private void checkRequiredFields(TechnologicalProcessVO technologicalProcessVO){
        if (StringUtils.isBlank(technologicalProcessVO.getProcessName())){
            throw new ServiceException("工艺流程名称不能为空!");
        }
    }
    /**
     * 删除工艺流程
     */
    @PreAuthorize("@ss.hasPermi('base:process:remove')")
    @Log(title = "工艺流程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(technologicalProcessService.deleteTechnologicalProcessByIds(ids));
    }
}
